package com.example.administrator.baikangxing.fragment;


import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.activity.PersonInfoActivity;
import com.example.administrator.baikangxing.activity.SettingActivity;
import com.example.administrator.baikangxing.activity.WatchActivity;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.ToastUtil;
import com.example.administrator.baikangxing.view.CircleImageView;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment implements View.OnClickListener {

    private LinearLayout me_ll_setting;
    private CircleImageView me_civ_head;
    private LinearLayout me_ll_watch;
    private LinearLayout me_ll_on_off;
    private Button me_bt_power_off;
    private Button me_bt_restart;
    private LinearLayout me_ll_search;
    private TextView me_tv_nickname;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public void initView() {
        me_ll_setting = findView(R.id.me_ll_setting);
        me_civ_head = findView(R.id.me_civ_head);
        me_ll_watch = findView(R.id.me_ll_watch);
        me_ll_on_off = findView(R.id.me_ll_on_off);
        me_bt_power_off = findView(R.id.me_bt_power_off);
        me_bt_restart = findView(R.id.me_bt_restart);
        me_ll_search = findView(R.id.me_ll_search);
        me_tv_nickname = findView(R.id.me_tv_nickname);
        me_ll_search.setOnClickListener(this);
        me_bt_restart.setOnClickListener(this);
        me_bt_power_off.setOnClickListener(this);
        me_ll_watch.setOnClickListener(this);
        me_ll_on_off.setOnClickListener(this);
        me_civ_head.setOnClickListener(this);
        me_ll_setting.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        me_tv_nickname.setText(OperateData.getStringData(getContext(), Constants.USER_NICKNAME));
        if(!OperateData.getStringData(getContext(),Constants.USER_IMAGE).equals("null")){
            Picasso.with(getContext()).load(OperateData.getStringData(getContext(), Constants.USER_IMAGE)).into(me_civ_head);
        }else {
            Picasso.with(getContext()).load(R.drawable.head).into(me_civ_head);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.me_ll_setting://系统设置
                startActivity(new Intent(getActivity(), SettingActivity.class));
                break;
            case R.id.me_civ_head://个人信息
                startActivity(new Intent(getActivity(), PersonInfoActivity.class));
                break;
            case R.id.me_ll_watch://手表信息
                startActivity(new Intent(getActivity(), WatchActivity.class));
                break;
            case R.id.me_ll_search://寻人启事
                break;
            case R.id.me_bt_power_off://关闭设备
                showDialog("poweroff");
                break;
            case R.id.me_bt_restart://重启设备
                showDialog("reboot");
                break;
            case R.id.me_ll_on_off://远程开关
                break;
        }
    }

    private void showDialog(final String reboot) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage("是否控制设备？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                powerOff(reboot);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.create().show();
    }


    private void powerOff(String key) {//关闭重启设备
        String[] values = new String[]{OperateData.getStringData(getContext(),Constants.SELECT_DEVICE_NUM),key};
        HttpUtil.getInstance().postString(Url.power_manage, new String[]{"devid", "ext"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    if(successString.getString("errorCode").equals("0")){
                        ToastUtil.showToast("远程控制成功");
                    }else {
                        ToastUtil.showToast(successString.getString("errorInfo"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }
}
